package com.example.demomcp.service;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Scanner;

@Service
public class ChromeService {
    public static   String chromePath = "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";

    @Tool(description = "打开浏览器")
    public String openChromeTool() {
        openGoogleChrome();
        return "已经打开啦";
    }
    @Tool(description = "浏览器中搜索")
    public String searchChromeTool(@ToolParam(required = true,description = "搜索引擎")
                                           String searchEngine
            ,@ToolParam(required = true,description = "搜索内容") String query) {
        searchInChrome(searchEngine,query);
        return "已经搜索啦";
    }
    private static void openGoogleChrome() {
        try {
            // 指定 Google Chrome 的安装路径
//            String chromePath = "C:\\Users\\Administrator\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"; // Windows 示例路径
            // 对于 macOS 或 Linux，路径会有所不同，例如 "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"

            ProcessBuilder processBuilder = new ProcessBuilder(chromePath);
            processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void takeScreenshot() {
        try {
            Robot robot = new Robot();
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage screenFullImage = robot.createScreenCapture(screenRect);

            // 保存截图到文件
            File screenshotFile = new File("screenshot.png");
            ImageIO.write(screenFullImage, "png", screenshotFile);

            System.out.println("截图已保存为: " + screenshotFile.getAbsolutePath());

        } catch (AWTException | IOException ex) {
            System.err.println(ex);
        }
    }

    private static void searchInChrome(String searchEngine,String query) {
        try {
//            String chromePath = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"; // Windows 示例路径
            //https://www.baidu.com/s?wd=
            //https://www.google.com/search?q=
//            String searchUrl = searchEngine+"/search?q=" + URLEncoder.encode(query, "UTF-8");
            String searchUrl = "www.baidu.com/s?wd=" + URLEncoder.encode(query, "UTF-8");
            ProcessBuilder processBuilder = new ProcessBuilder(chromePath, searchUrl);
            processBuilder.start();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            // 可能需要导入 java.net.URLEncoder 并处理编码异常
            e.printStackTrace();
        }
    }
}
